package com.application.meizhi.ui.activity;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import com.application.meizhi.R;
import com.application.meizhi.persenter.SplashActivityPresenter;
import com.application.meizhi.ui.iView.ISplashView;
import com.orhanobut.logger.Logger;


import net.youmi.android.AdManager;
import net.youmi.android.normal.common.ErrorCode;
import net.youmi.android.normal.spot.SplashViewSettings;
import net.youmi.android.normal.spot.SpotListener;
import net.youmi.android.normal.spot.SpotManager;

public class SplashActivity extends BaseActivity<SplashActivityPresenter> implements ISplashView {
    private Context mContext;

    @Override
    public void initView() {
        mContext = this;


        // 设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 移除标题栏
        // requestWindowFeature(Window.FEATURE_NO_TITLE);

        runApp();
    }

    @Override
    protected void initPresenter() {
        presenter = new SplashActivityPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }


    /**
     * 跑应用的逻辑
     */
    private void runApp() {
        //有米广告初始化
        setupSplashAd();
    }

    /**
     * 设置开屏广告
     */
    private void setupSplashAd() {
        // 创建开屏容器
        final RelativeLayout splashLayout = (RelativeLayout) findViewById(R.id.rl_splash);
        RelativeLayout.LayoutParams params =
                new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.ABOVE, R.id.view_divider);

        // 对开屏进行设置
        SplashViewSettings splashViewSettings = new SplashViewSettings();
        // 设置跳转的窗口类
        splashViewSettings.setTargetClass(MainActivity.class);
        // 设置开屏的容器
        splashViewSettings.setSplashViewContainer(splashLayout);


        // 展示开屏广告
        SpotManager.getInstance(mContext)
                .showSplash(mContext, splashViewSettings, new SpotListener() {

                    @Override
                    public void onShowSuccess() {
                        Logger.d("开屏展示成功");
                        splashLayout.setVisibility(View.VISIBLE);
                        splashLayout.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_splash_enter));
                    }

                    @Override
                    public void onShowFailed(int errorCode) {
                        Logger.d("开屏展示失败---->errorCode:" + errorCode);
                        switch (errorCode) {
                            case ErrorCode.NON_NETWORK:
                                Logger.e("无网络");
                                break;
                            case ErrorCode.NON_AD:
                                Logger.e("无广告");
                                break;
                            case ErrorCode.RESOURCE_NOT_READY:
                                Logger.e("资源还没准备好");
                                break;
                            case ErrorCode.SHOW_INTERVAL_LIMITED:
                                Logger.e("展示间隔限制");
                                break;
                            case ErrorCode.WIDGET_NOT_IN_VISIBILITY_STATE:
                                Logger.e("控件处在不可见状态");
                                break;
                        }
                    }

                    @Override
                    public void onSpotClosed() {
                        Logger.e("开屏被关闭");
                        finish();
                    }

                    @Override
                    public void onSpotClicked(boolean isWebPage) {
                        Logger.e("开屏被点击");
                        Logger.e(String.format("是否是网页广告？%s", isWebPage ? "是" : "不是"));
                    }
                });
    }

    @Override
    protected void onStop() {
        super.onStop();

    }
}
